﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Linq.Mapping;
using System.ComponentModel;
using System.Data.Linq;

namespace STS.Model
{
    [Table]
    public class PoraIlosc : INotifyPropertyChanged, INotifyPropertyChanging
    {
        private int id;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int Id
        {
            get { return id; }
            set
            {
                if (id != value)
                {
                    NotifyPropertyChanging("Id");
                    id = value;
                    NotifyPropertyChanged("Id");
                }
            }
        }

        [Column]
        internal int _lekId;
        
        private EntityRef<Lek> _lek;

        [Association(Storage = "_lek", ThisKey = "_lekId", OtherKey = "Id", IsForeignKey = true, DeleteOnNull = true)]
        public Lek Lek
        {
            get { return _lek.Entity; }
            set
            {
                Lek previousValue = _lek.Entity;
                if (previousValue != value || (_lek.HasLoadedOrAssignedValue == false))
                {
                    NotifyPropertyChanging("Lek");

                    if (previousValue != null)
                    {
                        _lek.Entity = null;
                        previousValue.PoraIloscArray.Remove(this);
                    }

                    _lek.Entity = value;

                    if (value != null)
                    {
                        _lekId = value.Id;
                        value.PoraIloscArray.Add(this);
                    }

                    NotifyPropertyChanged("Lek");
                }
            }
        }

        private int ilosc;

        [Column]
        public int Ilosc
        {
            get { return ilosc; }
            set
            {
                if (ilosc != value)
                {
                    NotifyPropertyChanging("Ilosc");
                    ilosc = value;
                    NotifyPropertyChanged("Ilosc");
                }
            }
        }

        private int nazwa;

        [Column]
        public int Nazwa
        {
            get { return nazwa; }
            set
            {
                if (nazwa != value)
                {
                    NotifyPropertyChanging("Nazwa");
                    nazwa = value;
                    NotifyPropertyChanged("Nazwa");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public event PropertyChangingEventHandler PropertyChanging;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        public override string ToString()
        {
            return String.Format("Id: {0} Nazwa: {1} Ilosc: {2} Lek.id: {3}", Id, Nazwa, Ilosc, Lek.Id);
        }
    }
}
